if lib and not lib.IsChart then


function lib:IsChart(tag)
	return int(getNumCandles(tag)) > 0
end


function lib:GetChartInstrument(tag)
	local name = self:GetChartInstrumentName(tag)
	local instrument = self:GetInstrument(name)
	return instrument
end


function lib:GetChartInstrumentName(tag)
	local t, n, l = getCandlesByIndex(tag, 0, 0, 1)
	if n < 1 then
		return
	end
	--message(l)
	l = string.gsub(l, "%s+Price.*", "")
	l = string.gsub(l, "%s+[[].*", "")
	return l
end


function lib:GetChartInterval(tag)		-- chart interval mins
	if type(tag) == "string" then
		local t, n, l = getCandlesByIndex(tag, 0, 0, 5)
		if l then
			l = string.gsub(l, "%s*[[].*", "")
		end
		if n < 5 then return end
		local intervals = {{"month", "MN", -3}, {"day", "D", -1}, {"hour", "H", 60}, {"min", "M", 1}, {"sec", "T", 0}}
		local interval, d
		for i, v in ipairs(intervals) do
			interval = nil
			for i = 1, n - 1 do
				d = t[i].datetime[ v[1] ] - t[i - 1].datetime[ v[1] ]
				if d >= 0 and (not interval or interval > d) then
					interval = d
				end
			end
			if interval >= 1 then
				if v[3] > 0 then
					return interval * v[3], v[2] .. interval, l
				elseif v[3] < 0 then
					return v[3], v[2] .. interval, l
				else
					return v[3], v[2], l
				end
			elseif interval >= 0 and i == #intervals then
				return v[3], v[2], l
			end
		end
	end
	return 1
end


function lib:GetChartGraph(tag)		-- instrument name and time period
	local interval, sinterval, l = self:GetChartInterval(tag)
	if l and sinterval then return l .. " [" .. sinterval .. "]" end
end


function lib:IsChartList(...)
	local list = self:GetChartList(...)
	return #list > 0
end


function lib:GetChartList(...)
	local str
	local list = {}
	for i = 1, 20 do
		for j, id in ipairs({...}) do
			str = string.format(id .. "%02d", i)
			if lib:IsChart(str) then
				table.insert(list, str)
			end
		end
	end
	return list
end


function lib:SelectTab(index)
	index = int(index)
	if index > 0 then
		self:AddLine(self.path.events .. "chart.select", self:GetUser() .. "\t" .. index)
	end
end


function lib:SelectChartTab(index)
	self:SelectTab(index + self.global.first_chart_tab - 1)
end


end -- of file
